import useGlobal from '../../hooks/useGlobal';
import useDidMount from '../../hooks/useDidMount';
import * as reporterApi from '../../apis/report';
import {useImmerState} from '../../hooks/useImmerState';

const useReporterModel = () => {
  const {state: globalState} = useGlobal();
  const [state, setState] = useImmerState({
    list: []
  });

  const _getReport = async () => {
    const res = await reporterApi.getReport(globalState.login?.token);
    if (res.data.code === 1) {
      setState(draft => {
        const list = res.data.data.list;
        draft.list = list.map(item => {
          return {
            ...item,
            data_year: item.data_year?.map(year => ({name: year.value, value: year.total})) || []
          };
        });
      });
    }
  };

  useDidMount(() => {
    _getReport();
  });

  return {
    state
  };
};

export default useReporterModel;